﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MIMSDomain.Abstract;
using MIMSWeb.Models;
using MIMSDomain.Entities;
using Telerik.Web.Mvc.UI;
using MIMSCore.Logging;
using MIMSCore.Abstract;

namespace MIMSWeb.Controllers
{
    [Authorize]
    public class PatientsController : Controller
    {
        IRepository<Patient> _patientRepository;
        ILogger<SynkronLogger> _logger;

        public PatientsController(IRepository<Patient> patientRepository , ILogger<SynkronLogger> logger)
        {
            _patientRepository = patientRepository;
            _logger = logger;
        }

        //List all patients in a Grid
        public ActionResult Index()
        {
            var model = _patientRepository.All;
            return View(model);
        }

        public ActionResult AddPatient()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddPatient(Patient Model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _patientRepository.Save(Model);
                    _logger.WriteInformation(2000, "Patient Created Successfully: ",
                            new Dictionary<string, object> { { "Personnel Details", Model } });
                }
                catch (Exception Ex)
                {
                    _logger.WriteCritical(4000, "Error Adding Patient", Ex);
                    ModelState.AddModelError("", Ex.Message);
                }

            }

            return View();
        }

        public ActionResult Patient(int Id)
        {
            try
            {
                var patient = _patientRepository.GetById(Id);
                _logger.WriteInformation(2000, "Patient Detail Retrieved Successfully: ", new Dictionary<string, object> { 
                    { "Patient Information", patient } });
                
                return View(patient);
            }
            catch (Exception Ex)
            {
                _logger.WriteCritical(4000, "Error Retrieving Patient", Ex);
                ModelState.AddModelError("", Ex.Message);
            }

            return View();
        }

        [HttpPost]
        public ActionResult Patient(Patient objPatient)
        {
            try
            {
                var p = (from t in _patientRepository.All
                         where t.Id == objPatient.Id
                         select t).SingleOrDefault();
                _logger.WriteInformation(2000, "Patient Detail Updated Successfully: ", new Dictionary<string, object> { 
                    { "Patient Information", p } });
                return View(p);
            }
            catch(Exception Ex){
                _logger.WriteCritical(4000, "Error Updating Patient Information", Ex);
                ModelState.AddModelError("", Ex.Message);
            }

            return View();
        }
        public JsonResult GetPatients()
        {
            //var patients = _patientRepository.Patients.ToList();

            return Json(new
            {
                Id = 1,
                FirstName = "Oladipo",
                LastName = "Olasemo",
                MiddleName = "Akindeji"
            }, JsonRequestBehavior.AllowGet);
            //return new JsonResult();
        }

        public ActionResult DeletePatient(int Id)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //retrieve patient with Id
                    var objPatient = _patientRepository.GetById(Id);
                    _patientRepository.Delete(objPatient);
                    _logger.WriteInformation(2000, "Patient Deleted Successfully", new Dictionary<string, object> { 
                    { "Patient Information", objPatient } });
                    
                }
                catch(Exception Ex)
                {
                    _logger.WriteCritical(4000, "Error Deleting Patient Information", Ex);
                    ModelState.AddModelError("", Ex.Message);
                }
            }

            return View();
        }

        public ActionResult EditPatient(int Id)
        {
            var objPatient = _patientRepository.GetById(Id);
            return View(objPatient);
        }

        [HttpPost]
        public ActionResult EditPatient(Patient objPatient)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _patientRepository.Save(objPatient);

                    _logger.WriteInformation(2000, "Patient Detail Updated Successfully: ", new Dictionary<string, object> { 
                    { "Patient Information", objPatient} });

                }
                catch(Exception Ex)
                {
                    ModelState.AddModelError("", Ex.Message);
                    _logger.WriteCritical(4000, "Error Updating Patient Information", Ex);
                }

            }
            return View();
        }

    }
}
